//
//  BaseHTTPDataManger.m
//  TBNetworking_Example
//
//  Created by apple on 2018/8/8.
//  Copyright © 2018年 lzb. All rights reserved.
//

#import "BaseHTTPDataManger.h"

@implementation BaseHTTPDataManger
- (NSURLSessionDataTask *)sendGetWithModel:(BaseResquestModel *)requestModel
                             ResponseClass:(Class)responseClass
                            sucessResponse:(sucessResponseBlock)sucessBlock
                              failResponse:(failResponseBlock)failBlock
{
    AFNetWorkRequestTool *requestTool = [AFNetWorkRequestTool shareInstance];
     __weak typeof(self) weakSelf = self;
   NSURLSessionDataTask *task = [requestTool httpRequestWithModel:requestModel ResponseClass:responseClass isRequireAnalysis:YES requestMehtod:AFNetworkRequestMethod_GET sucessResponse:^(id response) {
         [weakSelf callback:response error:nil sucess:sucessBlock fail:failBlock];
    } failResponse:^(NSError *error) {
        [weakSelf callback:nil error:error sucess:sucessBlock fail:failBlock];
    }];
    [task resume];
    return task;
    
}



- (NSURLSessionDataTask *)sendPostWithModel:(BaseResquestModel *)requestModel
                              ResponseClass:(Class)responseClass
                             sucessResponse:(sucessResponseBlock)sucessBlock
                               failResponse:(failResponseBlock)failBlock
{
    AFNetWorkRequestTool *requestTool = [AFNetWorkRequestTool shareInstance];
    __weak typeof(self) weakSelf = self;
    NSURLSessionDataTask *task = [requestTool httpRequestWithModel:requestModel ResponseClass:responseClass isRequireAnalysis:YES requestMehtod:AFNetworkRequestMethod_POST sucessResponse:^(id response) {
        [weakSelf callback:response error:nil sucess:sucessBlock fail:failBlock];
    } failResponse:^(NSError *error) {
        [weakSelf callback:nil error:error sucess:sucessBlock fail:failBlock];
    }];
    [task resume];
    return task;
}

//回调处理
- (void)callback:(id)data error:(NSError *)error sucess:(void(^)(id))sucess fail:(void(^)(NSError *error))fail
{
    if(error && fail)
    {
        fail(error);
    }
    else
    {
        if(sucess)
            sucess(data);
    }
}
@end
